package cn.maihe.elg.operation.supports.electronicseal.anne;


import cn.maihe.elg.operation.model.enums.ContractType;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.base.SecureApiResponse;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.notice.ContractSignerStatusChangeNotifyRequest;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.notice.EnterpriseStatusChangeNotifyRequest;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.notice.NotifyBaseRequest;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.AnneSignConst;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.JsonUtils;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.NotifyConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/callback/annesign")
@Slf4j
public class AnneSignAction {


    @ResponseBody
    @RequestMapping(value = "notify", method = RequestMethod.POST)
    public SecureApiResponse notify(@RequestBody String requestJson) {
        log.info("annesign push data：" + requestJson);
//        String secretKey = AnneSignService.getSecretKey();
        String secretKey = "AnneSignService.getSecretKey();";
        NotifyBaseRequest request = JsonUtils.fromJson(requestJson, NotifyBaseRequest.class);
        if (!request.getSign(secretKey).equals(request.getSign())) {
            return setSecureApiResponse(request.getUuid(), secretKey, AnneSignConst.RESPONSE_FAIL_CODE, "验签失败");
        }
        if (NotifyConst.BIZ_TYPE_CONTRACT.equals(request.getBody().getBizType())) {
            if (NotifyConst.MSG_TYPE_SIGNER_STATUS_CHANGE.equals(request.getBody().getMsgType())) {
                return changeContractStatus(requestJson, secretKey);
            }
        } else if (NotifyConst.BIZ_TYPE_ENTERPRISE.equals(request.getBody().getBizType())) {
            if (NotifyConst.MSG_TYPE_STATUS_CHANGE.equals(request.getBody().getMsgType())) {
                return changeEnterpriseStatus(requestJson, secretKey);
            }
        }
        return setSecureApiResponseSuccess(request.getUuid(), secretKey);
    }

    private SecureApiResponse changeEnterpriseStatus(String requestJson, String secretKey) {
        EnterpriseStatusChangeNotifyRequest request = JsonUtils.fromJson(requestJson, EnterpriseStatusChangeNotifyRequest.class);
        String status = request.getBody().getStatus();
        String auditOpinion = request.getBody().getAuditOpinion();
        String licenseCode = request.getBody().getLicenseCode();
        Long enterpriseId = request.getBody().getEnterpriseId();
        //TODO

        return setSecureApiResponseSuccess(request.getUuid(), secretKey);
    }

    private SecureApiResponse changeContractStatus(String requestJson, String secretKey) {
        ContractSignerStatusChangeNotifyRequest request = JsonUtils.fromJson(requestJson, ContractSignerStatusChangeNotifyRequest.class);
        String contractStatus = request.getBody().getContractStatus();
        Long contractId = request.getBody().getContractId();
        String thirdContractId = request.getBody().getThirdContractId();
        if (thirdContractId.startsWith(ContractType.BH.name())) {
//            orderService.updateAnneSignContractStatus(contractStatus, contractId);
        } else if (thirdContractId.startsWith(ContractType.SQ.name())) {
//            cooperateCompanyService.updateAnneSignContractStatus(contractStatus, contractId);
        }
        return setSecureApiResponseSuccess(request.getUuid(), secretKey);
    }

    private SecureApiResponse setSecureApiResponseSuccess(String secretKey, String uuid) {
        return setSecureApiResponse(secretKey, uuid, AnneSignConst.RESPONSE_OK_CODE, AnneSignConst.RESPONSE_OK_DETAIL);
    }

    private SecureApiResponse setSecureApiResponse(String secretKey, String uuid, Integer code, String detail) {
        SecureApiResponse response = new SecureApiResponse();
        response.setTimestamp(System.currentTimeMillis());
        response.setUuid(uuid);
        response.setVersion(AnneSignConst.API_VERSION_1);
        response.setDetail(detail);
        response.setCode(code);
        response.setSign(response.getSign(secretKey));
        return response;
    }
}
